import logging
from logging.handlers import RotatingFileHandler
import os
from excel_pytest_allure_class.common import read_yaml


log_path = read_yaml.log_path


def get_logger(log_name):
    logger = logging.getLogger(log_name)
    # 收集日志级别
    logger.setLevel('INFO')
    fmt = '%(asctime)s - %(name)s - %(levelname)s -  %(filename)s - %(message)s'
    log_formate = logging.Formatter(fmt)
    # 日志写入路径
    file_name = os.path.join(log_path, log_name)
    file_handler = RotatingFileHandler(file_name, maxBytes=20 * 1024 * 1024, backupCount=10,encoding='utf-8')
    file_handler.setLevel('INFO')

    # 日志输出的级别
    stream_handler = logging.StreamHandler()
    stream_handler.setLevel('INFO')
    # 指定日志格式
    stream_handler.setFormatter(log_formate)
    file_handler.setFormatter(log_formate)

    logger.addHandler(file_handler)
    logger.addHandler(stream_handler)

    return logger

if __name__ == '__main__':
    log = get_logger('case.log')
    log.info("这是一个info日志")